The namedItem method must be mentioned for HTMLCollection objects.
Before you begin reading this article, I strongly recommend that you read this article: http://w3help.org/zh-cn/causes/SD9004.
HTMLCollection Interface Definition
Yesterday saw a queryselectorall () thing, found that the use of similar to the Getelementsby series, they want to delve into the differences between them, the following is the information found.
PS: The reason to understand this knowledge point
Two
Transferred from: http://www.jb51.net/article/24182.htmvar anchors = document.getElementsByTagName ("a"); for (i = 0; i ) {var ele=anchors[i]; // take an element // some code hereThe above code means getting all the link elements in the document
Like what:
Copy Code code as follows:
var anchors = document.getElementsByTagName ("a");
for (i = 0; i var ele=anchors[i];//takes an element
Some code here
}
The above code represents getting all the link elements in
In front-end web programming, we usually use the document. getelementsbytagname method to retrieve a group of DOM elements with the same tag, such:
VaR anchors = Document. getelementsbytagname ("");For (I = 0; I VaR ele = anchors [I]; // obtain an
For example:
CopyCode The Code is as follows: var anchors = Document. getelementsbytagname ("");
For (I = 0; I VaR ele = anchors [I]; // obtain an element
// Some code here
}
The code above indicates getting all the link elements in the document,
In a recent reading of JavaScript advanced programming, I saw the phrase: "Understanding nodelist and Htmlcollection is the key to a thorough understanding of the DOM in general." , so I think we should write a blog about nodelist and htmlcollection
In Web Front-end programming, we usually use the document. getElementsByTagName method to retrieve a group of dom elements with the same tag. For example:
The Code is as follows:
Var anchors = document. getElementsByTagName ("");For (I = 0; I Var
In the console typeof NodeList
The result of the execution is: function, and the results are entered in the console nodelist, the effect is as follows:
found that in addition to function inside nothing, so directly new nodelist, the effect is as
First look at what is nodelist,nodelist and arguments are not normal arrays, they have some basic properties of the array but not exactly an array. Here is the definition found on Mozilla:
This is a commonly used type which be a collection of nodes
In Web front-end programming, we usually take a document.getelementsbytagname approach to get a set of DOM elements of the same label, such as:
var list = document.getElementsByTagName ("li");
for (i = 0; i
First, this group of DOM elements
querySelector()。 This method can be called by the Document or element type.When a method is called with the document type, a matching element is found within the scope of the querySelector() documentation element, and when the method is called with
Node objects are a single node, but sometimes a data structure is required to accommodate multiple nodes. The DOM provides two interfaces for deploying a collection of such nodes, namely NodeList and htmlcollection
Definition on MDN:
When getting native DOM elements, it mainly involves these Dom APIs (linked to living standard):
Nodeand corresponding setsNodeList
Element(Inheritance Node ) and corresponding setsHTMLCollection
Document(Inherited Node )
Note:
Main API that involves getting elementsWhen getting native DOM elements, it mainly involves these Dom APIs (linked to living standard):
Nodeand corresponding setsNodeList
Element(Inheritance Node ) and corresponding setsHTMLCollection
NodeListThe NodeList instance object is a class array object whose members are node objects, including the ChildNodes and Queryselectorall () method return valuesA dynamic collection is a change in the DOM structure that can be automatically
nodelist
Each node has a ChildNodes property that holds the NodeList object. NodeList is a class array object that holds an ordered set of nodes that can be accessed by location. The uniqueness of the NodeList object is that it is actually the
function Getdatatype (any){/* (1) Object.prototype.toString.call method judgment type:Pros: Generic, returns the type of "[Object String]" specific objectDisadvantage: Cannot return an inherited type (2) typeof XDisadvantage: The object type can not
In JS, through document.getElementsByTagName ("tag"), get a NodeList object, for this NodeList object we can use subscript way to access, It is easy to use the NodeList object as an array or in an array to be used as an NodeList object, and then
The book says: Each node has a ChildNodes property that holds the NodeList object. NodeList is an "object of class array". NodeList is a living, breathing object. Rather than a snapshot of an instant at the time of our first visit. nodelist
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.